package com.huadao.cart_ccs.base;

import android.content.Context;
import android.content.SharedPreferences;

import com.huadao.cart_ccs.Constant;
import com.huadao.cart_ccs.data.bean.SerialPortInfo;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.main.CartApplication;

import org.greenrobot.greendao.async.AsyncSession;

import static android.content.Context.MODE_PRIVATE;


/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
       Author   :  lixiaodaoaaa
       Date     :  2018/8/22
       Time     :  13:27
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class MainDataManager {

    private static MainDataManager instance;
    private Context context;

    private MainDataManager() {
        context = CartApplication.getInstance().getApplicationContext();
    }

    public static MainDataManager getInstance() {
        if (instance == null) {
            instance = new MainDataManager();
        }
        return instance;
    }

    public AsyncSession getAsyncSession() {
        return GreenDaoManager.getInstance().getAsyncNoCacheSession();
    }

    public SerialPortInfo getSerialPortInfo() {
        SharedPreferences sp = context.getSharedPreferences(Constant.PREFERCENCE_FILE_NAME, MODE_PRIVATE);
        String device = sp.getString(Constant.KEY_DEVICE, "");
        int baudrate = Integer.decode(sp.getString(Constant.KEY_BAUDRATE, "-1"));
        return new SerialPortInfo(device, baudrate);
    }


}
